<?php
namespace app\manage\controller;
use think\Db;

class UserTest extends Base
{
    protected function check($url, $params=[]){
        $params2 = ['clientId'=>113];
        $str = 'clientId=113';
        if(count($params)){
            $data = array_merge($params2, $params);
            ksort($data);
            $str = http_build_query($data);
        }
        $str .= '&secret=8e50d1bac9bf3e49cb7cca55b2133ec2';
        $data['sign'] = md5($str);
        $data['clientId'] = 113;
        $data['crypt'] = 'miyu';
        $http = http($url, $data);
        $json = json_decode($http, true);
        return $json;
    }

    public function index(){
        if( request()->isAjax() ){
            $myUserName = input('myUserName');
            $json = $this->check('http://dc.miyuhaiwai.com/Debug/getDebugers');

            if($myUserName){
                $json2 = [];
                foreach ($json as $k=>$v){
                    if($v['myUserName']==$myUserName){
                        $json2[] = $v;
                    }
                }
                $json = $json2;
                $count = count($json2);
            }else{
                $json = array_reverse($json);
                $count = count($json);
            }

            $this->layTable(0, '用户列表', $json, $count);
        }else{
            return view();
        }
    }

    public function add(){
        if( request()->isAjax() ){
            $myUserName = input('myUserName');
            $password = input('password');

            $json = $this->check('http://dc.miyuhaiwai.com/Debug/addDebuger', ['myUserName'=>$myUserName, 'password'=>sha1($password)]);

            $json['result'] ? $this->layJson(0, '添加成功') : $this->layJson(1, $json['msg']);
        }else{
            return view();
        }
    }

    //删除
    public function del(){
        $myUserId = input('myUserId');

        $json = $this->check('http://dc.miyuhaiwai.com/Debug/delDebuger', ['myUserId'=>$myUserId]);

        $json['result'] ? $this->layJson(0, '删除成功') : $this->layJson(1, $json['msg']);
    }
}